<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/19
 * Time: 15:18
 */

namespace app\common\mysic\bonus;


use app\common\library\wechat\WxPay;
use app\common\model\divide\Receive;
use app\common\mysic\MysicBase;
use app\mocks\extend\MockWeChatPay;
use think\Exception;
use think\Model;

class BonusHandler extends MysicBase implements BonusHandleInterface
{
    /**
     * @var null 用户分红金额
     */
    protected $bonus = null;
    /**
     * @var BonusReceiveOp 用户分红模型操作器
     */
    protected $bonusSetting;
    protected $userOrder;
    /**
     * @var WxPay|MockWeChatPay
     */
    protected $api;
    protected $totalBonus;
    protected $seq = 0;
    protected $wxRefundData;

    public function getBonus()
    {
        if($this->bonus === null) {
            throw new Exception('getBonus() - 分红金额未进行计算，为空');
        }
        return $this->bonus;
    }

    public function getTotalBonus()
    {
        return $this->totalBonus;
    }

    public function setBonus($bonus)
    {
        $this->bonus = $bonus;
        return $this;
    }

    public function setRefundSeq($seq)
    {
        $this->seq = $seq ?? 0;
        return $this;
    }

    public function getWxRefundData()
    {
        return $this->wxRefundData;
    }

    public function setBonusSetting(Model $bonusSetting)
    {
        $this->bonusSetting = $bonusSetting;
        return $this;
    }

    public function setUserOrder(Model $order)
    {
        $this->userOrder = $order;
        return $this;
    }

    public function setApi($api)
    {
        $this->api = $api;
        return $this;
    }

    public function calculateBonus($userCount = 0)
    {
        try{
            $this->log->info('开始', '计算当次订单用户可分红金额');
            $rewardRate = bcdiv(strval($this->bonusSetting->rewardpercent), '100',2);
            $this->log->info($rewardRate, '分红拨比');
            $systemDividendRate = '0.01';
            $rewardRateExcludeSystemDividendRate = bcsub($rewardRate, $systemDividendRate,2);
            $this->log->info($rewardRateExcludeSystemDividendRate, '减扣掉平台分销比例后的实际拨比');
            $totalUserMoney = bcmul($this->userOrder->pay_price , $rewardRateExcludeSystemDividendRate,2);//用户总的分红金额
            $this->totalBonus = $totalUserMoney;
            $this->log->info($totalUserMoney, '用户总的分红金额');

            $userCount = $userCount + 1;
            $this->bonus =  bcdiv($totalUserMoney, $userCount, 2);
            $this->log->info(bcdiv($totalUserMoney, $userCount, 4), '当次订单用户可分红金额为');
        }catch(\Exception $e) {
            $this->log->error(exceptionLogFormat($e), '计算每个人可分红的金额抛出异常');
        }
        return $this;
    }

    public function refundApply()
    {
        $ret = $this->api->refund($this->userOrder->transaction_id, $this->userOrder->pay_price, $this->bonus);
        $this->wxRefundData = $ret ?? [];
        if($ret['return_code'] != 'SUCCESS') {
            $this->log->error($ret['return_msg'], '退款申请通信失败');
            return false;
        }
        if($ret['result_code'] != 'SUCCESS') {
            $this->log->error($ret['result_err_code_des'], '退款申请业务失败');
            return false;
        }
        return true;
    }


    public function run()
    {
        $ret = $this->refundApply();
        if (!$ret) {
            return false;
        }
        $ret = $this->refundQuery();
        if(!$ret) {
            return false;
        }
            // 未完
        return true;
    }

    protected function refundQuery()
    {
        $ret = $this->api->refundQuery($this->userOrder->transaction_id, $this->userOrder->pay_price, $this->bonus);
        if(!$ret) {
            sleep(3);
            $ret = $this->refundQuery();
            if($ret) {
                return true;
            }
        }
        return true;

    }




}